#!/bin/sh
#

apk add nginx

chmod +x /tmp/entrypoint

cp -av /tmp/entrypoint /sbin/
cp -av /tmp/nginx.conf /etc/nginx/http.d/default.conf

######

cd /var/www

if [ ! -d /var/www/api ]; then
    wget https://github.com/rehiy/dailyhot-api/archive/refs/heads/master.tar.gz
    tar xvf master.tar.gz && mv dailyhot-api-master api
    rm -f master.tar.gz
fi

cd /var/www/api

[ -d node_modules ] || npm i

######

cd /var/www

if [ ! -d /var/www/front-build ]; then
    wget https://github.com/rehiy/dailyhot-front/archive/refs/heads/master.tar.gz
    tar xvf master.tar.gz && mv dailyhot-front-master front-build
    rm -f master.tar.gz
fi

cd /var/www/front-build

sed -i "s|Copyright By|$(date +'%Y')|" src/components/Footer.vue
sed -i 's|"author": "imsyy"|"author": "APP_COPYRIGHT"|' package.json
sed -i 's|"github": "https://github.com/imsyy"|"github": "APP_COPYRIGHT_URL"|' package.json

echo "VITE_GLOBAL_API=/api" >.env
echo "VITE_ICP=APP_ICP" >>.env
echo "VITE_DIR=/" >>.env

[ -d node_modules ] || npm i
[ -d dist ] || npm run build

mv dist /var/www/front
rm -rf /var/www/front-build

######

rm -rf /tmp/*
